<?php

namespace app\admin\controller;

use app\admin\AdminBaseController;
use app\common\model\DianzanModel;
use app\common\model\NoticeModel;
use app\admin\validate\NoticeValidate;
use app\common\model\SettingModel;
use think\exception\ValidateException;

class Dy extends AdminBaseController
{

    /**
     * 列表
     */
    public function dianzan()
    {
        return view('', [
            'status_list' => DianzanModel::STATUS,
        ]);
    }

    public function listDianzan()
    {
        $map   = $this->dealEqualInput(['status'],$this->dealLikeInput(['operator']));
        $list  = DianzanModel::where($map)
            ->order(['promotion_time' => 'desc'])
            ->limit(input('limit'))
            ->page(input('page'))
            ->append(['status_text'])
            ->select();
        $count = DianzanModel::where($map)->count();
        if ($count == 0) {
            ajax_return(1, '未查询到数据');
        }
        list_return($list, $count);
    }

    public function delDianzan()
    {
        $id = input('id');
        DianzanModel::destroy($id);
        ajax_return();
    }

    /**
     * 编辑
     */
    public function dianzanForm()
    {
        $id   = input('id/d, 0');
        $info = DianzanModel::find($id);
        if (!empty($info)) {
            $info['promotion_list'] = json_encode($info['promotion_list']);
        }

        return view('', [
            'info'        => $info,
            'status_list' => DianzanModel::STATUS,
            'gt2000'      => SettingModel::getConfigValue('gt2000'),
            'lt2000'      => SettingModel::getConfigValue('lt2000'),
            'profit'      => SettingModel::getConfigValue('profit'),
        ]);
    }

    public function editDianzan()
    {
        $data = input('post.');
        if (empty($data['promotion_list'])) {
            ajax_return(1, '请输入点赞列表');
        }

        $finish    = 0;
        $valid     = 0;
        $income    = 0;
        $ac_income = 0;
        $de_income = 0;
        foreach ($data['promotion_list'] as $k => $v) {
            if (empty($v['finish'])) {
                ajax_return(1, '请输入完成点赞数');
            }
            if (empty($v['valid'])) {
                ajax_return(1, '请点击计算结果');
            }
            $finish    += $v['finish'];
            $valid     += $v['valid'];
            $income    += $v['income'];
            $ac_income += $v['ac_income'];
            $de_income += $v['de_income'];
        }
        $data['finish']       = $finish;
        $data['valid']        = $valid;
        $data['income']       = $income;
        $data['ac_income']    = $ac_income;
        $data['de_income']    = $de_income;
        $data['rated_income'] = floor($de_income / 2);
        $data['transfer_accounts'] = $data['income'] + $data['rated_income'];

        if (empty($data['id'])) {
            DianzanModel::create($data);
        } else {
            DianzanModel::update($data, ['id' => $data['id']]);
        }

        ajax_return();
    }

    public function exportDianzan()
    {
        $id = input('id/d', 0);
        if (empty($id)) {
            return '请选择导出的数据';
        }

        $info = DianzanModel::find($id);
        if (empty($info)) {
            return '该数据不存在';
        }

        $promotion_list = $info['promotion_list'];
        $table          = "<table border='1' spacing='0' cellpadding='0' cellspacing='0'>";
        $table          .= "<tr><th>完成点赞数</th><th>生效点赞数</th><th>推广人收益</th><th>备注</th></th>";
        foreach ($promotion_list as $k => $v) {
            $table .= "<tr>";
            $table .= "<td>{$v['finish']}</td>";
            $table .= "<td>{$v['valid']}</td>";
            $table .= "<td>{$v['income']}</td>";
            $table .= "<td>{$v['comment']}</td>";
            $table .= "</tr>";
        }
        $table .= "<tr><td>日期</td><td colspan='3' style='text-align: right'>{$info['promotion_time']}</td></tr>";
        $table .= "<tr><td>总收益</td><td colspan='3' style='text-align: right'>{$info['income']}</td></tr>";
        $table .= "<tr><td>操作人</td><td colspan='3' style='text-align: right'>{$info['operator']}</td></tr>";
        $table .= "<tr><td>备注</td><td colspan='3' style='text-align: right'>{$info['comment']}</td></tr>";
        $table .= "</table>";

        header("Content-Type: application/vnd.ms-excel; name='excel'");
        header("Content-type: application/octet-stream");
        header("Content-Disposition: attachment; filename=" . $info['operator'] . $info['promotion_time'] . time() . '.xls');
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        header("Expires: 0");
        exit($table);
    }
}